<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="postForm"
      label-position="right"
      label-width="150px"
      style="width: 600px; margin-left:50px;"
    >
      <el-form-item label="用户姓名" prop="authorName">
        <el-input v-model="postForm.authorName" clearable placeholder="用户姓名" maxlength="30" />
      </el-form-item>
      <el-form-item label="java Api 文件路径" prop="javaApiFilepath">
        <el-input v-model="postForm.javaApiFilepath" placeholder="java Api 文件路径" maxlength="200" />
      </el-form-item>
      <el-form-item label="java Biz 文件路径" prop="javaBizFilepath">
        <el-input v-model="postForm.javaBizFilepath" placeholder="java Biz 文件路径" maxlength="200" />
      </el-form-item>
      <el-form-item label="vue文件路径" prop="vueFilepath">
        <el-input v-model="postForm.vueFilepath" placeholder="vue文件路径" maxlength="200" />
      </el-form-item>
      <el-form-item label="模板文件路径" prop="codemodelFilepath">
        <el-input v-model="postForm.codemodelFilepath" placeholder="vue文件路径" maxlength="200" />
      </el-form-item>
      <el-form-item label="数据库类型" prop="databaseType">
        <el-select v-model="postForm.databaseType" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in databaseTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="back()">返回</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addGenConfig, updateGenConfig, getGenConfig } from '@/api/gen/config'
import msgUtil from '@/utils/msgUtil'
import routerUtil from '@/utils/routerUtil'
import ComboUtil from '@/utils/ComboUtil'

const defaultForm = {
  id: null,
  authorName: null,
  javaApiFilepath: null,
  javaBizFilpath: null,
  vueFilepath: null,
  codemodelFilepath: null,
  databaseType: 1
}
export default {
  name: 'GenConfigDetail',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      databaseTypeOptions: ComboUtil.databaseTypeOptions,
      rules: {
        authorName: [{ required: true, trigger: 'blur' }],
        javaApiFilepath: [{ required: true, trigger: 'blur' }],
        javaBizFilepath: [{ required: true, trigger: 'blur' }],
        vueFilepath: [{ required: true, trigger: 'blur' }],
        codemodelFilepath: [{ required: true, trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    if (this.isEdit) {
      this.getGenConfig(this.$route.query.id)
    } else {
      this.loading = false
    }
  },
  methods: {
    // 修改
    updateGenConfig() {
      updateGenConfig(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/gen/config/index')
      })
    },
    // 新增用户
    addGenConfig() {
      addGenConfig(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/gen/config/index')
      })
    },
    // 返回
    back() {
      routerUtil.backPath(this, '/gen/config/index')
    },
    // 提交form
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            this.updateGenConfig()
          } else {
            this.addGenConfig()
          }
        }
      })
    },
    // 获取当前信息
    async getGenConfig(id) {
      var json = { 'id': id }
      await getGenConfig(json).then(response => {
        this.postForm = response.data
      })
    }
  }
}
</script>
<style scoped>

</style>
